package GUI;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;

import java.awt.GridLayout;
import javax.swing.JSpinner;
import fachlogik.Gast;

public class GastEditor extends JPanel
{
	private JTextField tfVorname;
	private JTextField tfName;
	
	private SpinnerDateModel sdmGebdat = new SpinnerDateModel();
	
	private Gast gast;
	private JSpinner spGebdat;

	/**
	 * Create the panel.
	 */
	
	
	public GastEditor() 
	{
		initCompontents();
	}
	
	
	public GastEditor(Gast g)
	{
		initCompontents();
		setGast(g);
	}

	/**
	 * Es wird ein Attribut von Gast gefordert und diese, dann in das TF eingesetzt.
	 * @param gast
	 */
	
	private void setGast(Gast gast)
	{
		this.gast = gast;
		if (gast != null){
		tfName.setText(gast.getName());
		tfVorname.setText(gast.getVorname());
		sdmGebdat.setValue(gast.getGebdat());
		
		}
		else
		{
			tfName.setText("");
			tfVorname.setText("");
			sdmGebdat.setValue("");
		}
		
	}

	public Gast getGast()
	{
		gast.setName(tfName.getText());
		gast.setVorname(tfVorname.getText());
		gast.setGebdat(sdmGebdat.getDate());
		return gast;
	}
	

	private void initCompontents()
	{
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 2, 0, 0));
		
		JLabel lbName = new JLabel("Name");
		panel_2.add(lbName);
		
		tfName = new JTextField();
		panel_2.add(tfName);
		tfName.setColumns(10);
		
		JLabel lblVorname = new JLabel("Vorname");
		panel_2.add(lblVorname);
		
		tfVorname = new JTextField();
		panel_2.add(tfVorname);
		tfVorname.setColumns(10);
		
		JLabel lblGeburtsdatum = new JLabel("Geburtsdatum");
		panel_2.add(lblGeburtsdatum);
		
		spGebdat = new JSpinner(sdmGebdat);
		spGebdat.setEditor(new JSpinner.DateEditor(spGebdat, "dd.MM.yyyy"));
		panel_2.add(spGebdat);
	}
}
